package push.rtsp;

import org.bytedeco.javacpp.avcodec;
import org.bytedeco.javacv.FFmpegFrameRecorder;

/**
 * TODO
 *
 * @author zhuo.zeng
 * @version 1.0
 * @date 2021/9/16 13:44
 */
public class Recoder {

    public static void main(String[] args) throws Exception {
//        String rtmpURI = "rtmp://127.0.0.1:1935/app/test";
//        String rtmpURI = "rtsp://127.0.0.1/oflaDemo/sssssss";
//        String rtmpURI = "rtmp://localhost:1935/live/test";
//        String rtmpURI = "rtmp://172.16.6.32:1935/live/test";
//        String rtmpURI = "rtsp://172.16.6.32:1935/live/test";
        String rtmpURI = "rtsp://172.16.6.32:9999/live/test";
        int width = 1280, height = 720, sampleRate = 44100, channels = 2;
        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(rtmpURI, width, height, channels);
        // Video
        recorder.setInterleaved(true);
        recorder.setVideoOption("tune", "zerolatency");
        recorder.setVideoOption("preset", "ultrafast");
        recorder.setVideoOption("crf", "28");
//        recorder.setOption("rtsp_transport","tcp");
        recorder.setVideoBitrate(2000000);
        // H.264
        recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
        recorder.setFormat("flv");//rtmp : flv
        recorder.setFrameRate(25);
        recorder.setGopSize(25 * 2);
        // Audio
        recorder.setAudioOption("crf", "0");
        recorder.setAudioQuality(0);
        recorder.setAudioBitrate(192000);
        recorder.setSampleRate(sampleRate);
        recorder.setAudioChannels(channels);
        // AAC
        recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC);

        recorder.start();
        Thread vt = new Thread(new VideoRecorder(recorder, width, height));
        Thread at = new Thread(new AudioRecoder(recorder, sampleRate, channels));

        vt.start();
        at.start();

        vt.join();
        at.join();

        recorder.close();
    }
}
